package com.polaris.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.polaris.server.dto.StationQueryParam;
import com.polaris.server.pojo.TrafficStation;
import com.polaris.server.mapper.TrafficStationMapper;
import com.polaris.server.service.ITrafficStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author polaris
 * @since 2021-06-02
 */
@Service
public class TrafficStationServiceImpl extends ServiceImpl<TrafficStationMapper, TrafficStation> implements ITrafficStationService {
    private final TrafficStationMapper stationMapper;

    public TrafficStationServiceImpl(TrafficStationMapper stationMapper) {
        this.stationMapper = stationMapper;
    }

    /**
     * 获取station list
     *
     * @param queryParam
     * @param page
     * @return
     */
    @Override
    public IPage<TrafficStation> list(StationQueryParam queryParam, Page<TrafficStation> page) {
        if (StringUtils.hasText(queryParam.toString())){
            QueryWrapper<TrafficStation> queryWrapper = new QueryWrapper<>();
            Map<String, Object> map = BeanUtil.beanToMap(queryParam,true,false);
            queryWrapper.allEq(map,false);
            return stationMapper.selectPage(page, queryWrapper);
        }
        return null;
    }

    /**
     * 根据索引查询单个station
     *
     * @param queryParam
     * @return
     */
    @Override
    public TrafficStation get(StationQueryParam queryParam) {
        try{
            QueryWrapper<TrafficStation> queryWrapper = new QueryWrapper<>();
            Map<String, Object> map = BeanUtil.beanToMap(queryParam,true,false);
            queryWrapper.allEq(map);
            return stationMapper.selectOne(queryWrapper);
        }
        catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }
}
